using System.Collections.Generic;
using UnityEngine;

public class PocketTrigger : MonoBehaviour
{
	public static List<int> pocketTriggerBallList = new List<int>();

	[SerializeField]
	private Transform m_RefTrans;

	[SerializeField]
	private PocketIndexes m_PocketIndex;

	private GameObject m_Punishment;

	private GameObject m_Reward;

	private GameObject m_Block;

	private Transform m_RealPosition;

	public static PocketIndexes PunitivePocket;

	public static PocketIndexes RewardPocket;

	[SerializeField]
	private bool m_debugmode;

	[SerializeField]
	private Transform m_PointerTransform;

	[SerializeField]
	private bool m_DrawGizmo;

	protected Transform _cacheTrans;

	public PocketIndexes PocketIndex => m_PocketIndex;

	public Vector3 GetBestPoint(Vector3 refPoint)
	{
		if ((m_PointerTransform.position - refPoint).sqrMagnitude < Pools.CueBall.GetRadius() * Pools.CueBall.GetRadius() * 4f)
		{
			Vector3 position = base.transform.position;
			position.y = refPoint.y;
			return position;
		}
		return m_PointerTransform.position;
	}

	private void Awake()
	{
		_cacheTrans = base.transform;
		m_Punishment = base.transform.Find("PunishmentSprite").gameObject;
		m_Reward = base.transform.Find("RewardSprite").gameObject;
		m_Block = base.transform.Find("BlockOff").gameObject;
		m_RealPosition = base.transform.Find("RealPosition");
		m_Punishment.gameObject.SetActive(value: false);
		m_Reward.gameObject.SetActive(value: false);
		if (!m_debugmode)
		{
			m_Block.gameObject.SetActive(value: false);
		}
		m_RefTrans.GetComponent<PoolRecycler>().SetTrigger(this);
	}

	public void Clear()
	{
		m_Punishment.gameObject.SetActive(value: false);
		m_Reward.gameObject.SetActive(value: false);
	}

	public void Punishment()
	{
		if (!m_Block.activeInHierarchy)
		{
			m_Reward.gameObject.SetActive(value: false);
			m_Punishment.gameObject.SetActive(value: true);
		}
	}

	public void Reward()
	{
		if (!m_Block.activeInHierarchy)
		{
			m_Reward.gameObject.SetActive(value: true);
			m_Punishment.gameObject.SetActive(value: false);
		}
	}

	public void BlockOff()
	{
		m_Block.gameObject.SetActive(value: true);
	}

	public void OnCollisionEnter(Collision collision)
	{
		if (collision.transform.CompareTag("Ball") && !pocketTriggerBallList.Contains(collision.collider.GetInstanceID()))
		{
			AudioManager.PottedBall();
			collision.transform.GetComponent<PoolBall>().CloseRenderer();
			pocketTriggerBallList.Add(collision.collider.GetInstanceID());
		}
	}

	public Vector3 GetRealPosition()
	{
		return m_RealPosition.position;
	}

	public static void MarkPocketType(PocketIndexes punishmentIndex, PocketIndexes rewardIndex)
	{
		int i = 0;
		for (int count = Pools.PocketTriggers.Count; i < count; i++)
		{
			PocketTrigger pocketTrigger = Pools.PocketTriggers[i];
			if ((pocketTrigger.PocketIndex & punishmentIndex) != 0)
			{
				pocketTrigger.Punishment();
			}
			else if ((pocketTrigger.PocketIndex & rewardIndex) != 0)
			{
				pocketTrigger.Reward();
			}
			else
			{
				pocketTrigger.Clear();
			}
		}
		PunitivePocket = punishmentIndex;
		RewardPocket = rewardIndex;
	}

	public static void Block(PocketIndexes pockets)
	{
		int i = 0;
		for (int count = Pools.PocketTriggers.Count; i < count; i++)
		{
			PocketTrigger pocketTrigger = Pools.PocketTriggers[i];
			if ((pocketTrigger.PocketIndex & pockets) != 0)
			{
				pocketTrigger.BlockOff();
			}
		}
	}

	public static PocketTrigger GetPocketWithIndexes(PocketIndexes index)
	{
		int i = 0;
		for (int count = Pools.PocketTriggers.Count; i < count; i++)
		{
			if ((Pools.PocketTriggers[i].m_PocketIndex & index) != 0)
			{
				return Pools.PocketTriggers[i];
			}
		}
		return null;
	}

	public void OnDrawGizmos()
	{
		if (m_DrawGizmo)
		{
			Gizmos.color = Color.blue;
			Gizmos.DrawCube(m_PointerTransform.position, Vector3.one * 0.2f);
			Gizmos.color = Color.white;
		}
	}
}
